package com.myq.note.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

import java.security.MessageDigest;

@Component
public class Md5 {
    //将字符串加密后再返回一个加密后的字符串
    public static String getNewPwd(String password){
        //获取md5加密对象
        MessageDigest md5 = DigestUtils.getDigest("MD5");
        //将字符串加密为字节数组
        byte[] digest = md5.digest(password.getBytes());
        //使用Base64将字节数组抓变换为加密后的字符串
        String string = Base64.encodeBase64String(digest);
        return string;
    }

    public static String getSalt(){
        //返回一个随机字符串
        String str = "1234567890abcdefghijklmnopqretuvwxyz!@#$%^&*()ABCDEFGHIJKLMNOPQRETUVWXYZ";
        char[] ch = new char[5];
        for (int i = 0; i < 5; i++) {
            int random = (int) (Math.random()*72);
            char c = str.charAt(random);
            ch[i] = c;
        }
        return String.valueOf(ch);
    }
}
